Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.

fix: Return tool result in standard format - #52

Merged
Pederzh merged 1 commit into
mainfrom
fix/tool-result-standard-format
Oct 5, 2025
Merged

fix: Return tool result in standard format#52
Pederzh merged 1 commit into
mainfrom
fix/tool-result-standard-format

Conversation

@Pederzh

@Pederzh Pederzh commented Oct 5, 2025

Copy link
Copy Markdown
Contributor

Pull Request Description

Changes

Simplified tool result handling in the LangChain adapter by removing custom content parsing and returning the raw MCP tool result as JSON string.

The main improvement is that tool results with multiple content elements (like resources) will now be properly formatted as JSON strings in MCP format, rather than being incorrectly concatenated or causing parsing errors.

This change fixes issues with complex tool results containing multiple content elements and adds compatibility with MCP-UI servers.

Implementation Details

  1. Removed parseMcpToolResult function: Eliminated the complex content parsing logic that was manually handling different content types (text, image, resource)
  2. Simplified tool result return: Changed from parseMcpToolResult(result) to JSON.stringify(result) in the LangChain adapter
  3. Removed unused imports: Cleaned up imports by removing EmbeddedResource, ImageContent, and TextContent types that are no longer needed
  4. Preserved MCP format: Tool results now maintain their original MCP format structure instead of being flattened to strings

Example Usage (Before)

// Tool results were parsed and flattened to strings
function parseMcpToolResult(toolResult: CallToolResult): string {
  if (toolResult.isError) {
    throw new Error(`Tool execution failed: ${toolResult.content}`)
  }
  
  let decoded = ''
  for (const item of toolResult.content) {
    switch (item.type) {
      case 'text':
        decoded += (item as TextContent).text
        break
      case 'image':
        decoded += (item as ImageContent).data
        break
      case 'resource':
        // Complex resource handling logic
        break
    }
  }
  return decoded
}

// Usage in adapter
return parseMcpToolResult(result)

Example Usage (After)

// Tool results are returned as JSON strings preserving MCP format
const result: CallToolResult = await connector.callTool(mcpTool.name, input)
return JSON.stringify(result)

// This preserves the full MCP structure:
// {
//   "content": [
//     { "type": "text", "text": "Hello world" },
//     { "type": "resource", "resource": { ... } }
//   ],
//   "isError": false
// }

Documentation Updates

  • No documentation files were updated as this is an internal implementation change that maintains the same public API

Testing

Describe how you tested these changes:

  • Manual testing performed with MCP servers that return complex tool results with multiple content elements
  • Verified that tool results are properly preserved in their original MCP format
  • Tested compatibility with MCP-UI servers to ensure resources are displayed correctly
  • Edge cases considered: tools returning mixed content types (text + resources), empty content arrays, error responses

Backwards Compatibility

These changes are backwards compatible from an API perspective. The LangChain adapter still returns strings from tool execution, but now returns JSON-serialized MCP results instead of flattened text content. This change:

  • Maintains compatibility: Existing code using the adapter will continue to work
  • Improves functionality: Tools that return complex content (images, resources) now preserve their structure
  • Fixes display issues: MCP-UI servers can now properly render resources and mixed content types
  • No breaking changes: The public interface of the LangChain adapter remains unchanged

Related Issues

This change addresses issues with:

  • Tool results containing multiple content elements being improperly concatenated
  • Resources not displaying correctly in MCP-UI compatible interfaces
  • Loss of content type information when flattening complex tool results to strings
  • Compatibility issues with servers that expect full MCP format preservation

@Pederzh
Pederzh merged commit 085a20c into main Oct 5, 2025
1 check passed
@Pederzh
Pederzh deleted the fix/tool-result-standard-format branch October 5, 2025 16:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant